﻿using System;
using Shinkansen.Support;

namespace Shinkansen.Crunchers
{
    public class CruncherBuilder
    {
        private static readonly CruncherBuilder _instance = new CruncherBuilder ();

        private Func<ICssCruncher> _cssCruncherFactory;
        private Func<IJsCruncher> _jssCruncherFactory;

        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>The instance.</value>
        public static CruncherBuilder Current
        {
            get { return _instance; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CruncherBuilder"/> class.
        /// </summary>
        public CruncherBuilder ()
        {
            Reset ();
        }

        /// <summary>
        /// Resets this instance and sets default cruncher factories.
        /// </summary>
        public void Reset ()
        {
            _cssCruncherFactory = () => new MicrosoftCssCruncher ();
            _jssCruncherFactory = () => new MicrosoftJsCruncher ();
        }

        /// <summary>
        /// Gets the CSS cruncher.
        /// </summary>
        /// <returns></returns>
        public ICssCruncher GetCssCruncher ()
        {
            return _cssCruncherFactory ();
        }

        /// <summary>
        /// Gets the js cruncher.
        /// </summary>
        /// <returns></returns>
        public IJsCruncher GetJsCruncher ()
        {
            return _jssCruncherFactory ();
        }

        /// <summary>
        /// Sets the CSS cruncher.
        /// </summary>
        /// <param name="cssCruncher">The CSS cruncher.</param>
        public void SetCssCruncher (ICssCruncher cssCruncher)
        {
            if (cssCruncher == null)
                throw new ArgumentNullException ("cssCruncher");

            _cssCruncherFactory = () => cssCruncher;
        }

        /// <summary>
        /// Sets the js cruncher.
        /// </summary>
        /// <param name="jsCruncher">The js cruncher.</param>
        public void SetJsCruncher (IJsCruncher jsCruncher)
        {
            if (jsCruncher == null)
                throw new ArgumentNullException ("jsCruncher");

            _jssCruncherFactory = () => jsCruncher;
        }
    }
}